package controllers.customer;

import java.util.Collection;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.CustomerService;
import services.GalleryService;
import controllers.AbstractController;
import domain.Gallery;

@Controller
@RequestMapping("/gallery/customer")
public class GalleryCustomerController extends AbstractController{

	//Services
	@Autowired
	private GalleryService galleryService;
	@Autowired
	private CustomerService customerService;
	
	//Constructor
	
	public GalleryCustomerController(){
		super();
	}
	
	//Listing
	@RequestMapping(value = "/ownedList", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView result;
		Collection<Gallery>	galleries;

		galleries = galleryService.findByCustomer(customerService.findByPrincipal());

		result = new ModelAndView("gallery/ownedList");
		result.addObject("requestURI", "gallery/customer/ownedList.do");
		result.addObject("galleries", galleries);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}
	
	// Creation
		@RequestMapping(value = "/create", method = RequestMethod.GET)
		public ModelAndView create() {
			ModelAndView result;
			Gallery gallery;

			Boolean isNew;
			Boolean eraseable;

			gallery = galleryService.create();
			isNew = true;
			eraseable = false;

			result = new ModelAndView("gallery/create");
			result.addObject("gallery", gallery);
			result.addObject("requestURI", "gallery/customer/edit.do");
			result.addObject("isNew", isNew);
			result.addObject("eraseable", eraseable);
			result.addObject("backURI", "/welcome/index.do");

			return result;

		}

		// Edition
		@RequestMapping(value = "/edit", method = RequestMethod.GET)
		public ModelAndView edit(@RequestParam int galleryId) {
			ModelAndView result;
			Gallery gallery;
			Boolean isNew;
			Boolean eraseable;

			gallery = galleryService.findOne(galleryId);

			isNew = false;
			eraseable = gallery.getPaintings().isEmpty();

			result = new ModelAndView("gallery/edit");
			result.addObject("gallery", gallery);
			result.addObject("requestURI", "gallery/customer/edit.do");
			result.addObject("isNew", isNew);
			result.addObject("eraseable", eraseable);
			result.addObject("backURI", "/gallery/customer/ownedList.do");
			result.addObject("galleryId", galleryId);

			return result;

		}

		@RequestMapping(value = "/edit", method = RequestMethod.POST, params="save")
		public ModelAndView save(@Valid Gallery gallery,
				BindingResult binding) {
			ModelAndView result;
			Boolean isNew;
			Boolean eraseable;

			if (binding.hasErrors()) {
				result = createEditModelAndView(gallery);
			} else {
				try {
					galleryService.save(gallery);
					result = new ModelAndView("redirect:../../");
				} catch (Throwable oops) {
					result = createEditModelAndView(gallery,
							"gallery.commit.error");
				}
			}
			isNew = gallery.getId() == 0;
			eraseable = gallery.getPaintings().isEmpty();

			result.addObject("requestURI", "gallery/customer/edit.do");
			result.addObject("isNew", isNew);
			result.addObject("eraseable", eraseable);
			result.addObject("backURI", "/gallery/customer/ownedList.do");

			return result;
		}
		//Delete
		@RequestMapping(value = "/edit", method = RequestMethod.POST, params="delete")
		public ModelAndView delete(@Valid Gallery gallery,
				BindingResult binding) {
			ModelAndView result;
			Boolean isNew;
			Boolean eraseable;

			if (binding.hasErrors()) {
				result = createEditModelAndView(gallery);
			} else {
				try {
					galleryService.delete(gallery);
					result = new ModelAndView("redirect:../../");
				} catch (Throwable oops) {

					result = createEditModelAndView(gallery,
							"gallery.commit.error");
				}
			}
			isNew = gallery.getId() == 0;
			eraseable = gallery.getPaintings().isEmpty();

			result.addObject("requestURI", "gallery/customer/edit.do");
			result.addObject("isNew", isNew);
			result.addObject("eraseable", eraseable);
			result.addObject("backURI", "/gallery/customer/ownedList.do");

			return result;
		}
		// Ancillary methods
		protected ModelAndView createEditModelAndView(Gallery gallery) {
			ModelAndView result;

			result = createEditModelAndView(gallery, null);

			return result;
		}

		protected ModelAndView createEditModelAndView(Gallery gallery,
				String message) {
			ModelAndView result;

			result = new ModelAndView("gallery/edit");

			result.addObject("gallery", gallery);
			result.addObject("message", message);

			return result;
		}
}
